package com.yanglanting.disk.mybatis.flexigrid;

import com.yanglanting.disk.mybatis.annotation.Columns;

import java.io.Serializable;
import java.lang.reflect.Field;

public class PagingEntity implements Serializable {
    /**
     * 当前页
     */
    private Integer page;
    /**
     * 容量
     */
    private Integer rp;
    /**
     * 排序名称
     */
    private String sortname;
    /**
     * 升序降序
     */
    private String sortorder;

    private Integer start;

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getRp() {
        return rp;
    }

    public void setRp(Integer rp) {
        this.rp = rp;
    }

    public String getSortname() {
        return sortname;
    }

    public void setSortname(String sortname) {
        this.sortname = sortname;
    }

    public String getSortorder() {
        return sortorder;
    }

    public void setSortorder(String sortorder) {
        this.sortorder = sortorder;
    }

    public Integer getStart() {
        return (page-1)*this.rp;
    }

    public void deal(Class<?> classez,Integer total){
        if(total < (this.page-1) * this.rp){
            this.page = 1;
        }

        if("undefined".equals(this.sortname)){
            return;
        }

        try {
            Field field = classez.getDeclaredField(this.sortname);
            if(field.isAnnotationPresent(Columns.class)){
                Columns columns= field.getAnnotation(Columns.class);
                String val = columns.value();
                if(!"".equals(val)){
                    this.sortname = val;
                }
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString() {
        return "PagingEntity{" +
                "page=" + page +
                ", rp=" + rp +
                ", sortname='" + sortname + '\'' +
                ", sortorder='" + sortorder + '\'' +
                '}';
    }


}
